import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import BasicLayout from '../layout/BasicLayout.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

let getAction = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/account_manage',
    name: 'AccountManage',
    component: () => import('../views/AccountManage.vue')
  },
  {
    path: '/data_dictionary',
    name: 'DataDictionary',
    component: () => import('../views/DataDictionary.vue')
  },
  {
    path: '/carousel_manage',
    name: 'CarouselManage',
    component: () => import('../views/CarouselManage.vue')
  },
  {
    path: '/app_use',
    name: 'AppUser',
    component: () => import('../views/AppUser.vue')
  },
  {
    path: '/opinion',
    name: 'Opinion',
    component: () => import('../views/Opinion.vue')
  },
  {
    path: '/app_notice',
    name: 'AppNotice',
    component: () => import('../views/AppNotice.vue')
  },
  {
    path: '/app_coach',
    name: 'AppCoach',
    component: () => import('../views/AppCoach.vue')
  },
  {
    path: '/app_venue',
    name: 'AppVenue',
    component: () => import('../views/AppVenue.vue')
  },
  {
    path: '/app_class',
    name: 'AppClass',
    component: () => import('../views/AppClass.vue')
  }
]
const routes = [
  {
    path: '/home',
    component: BasicLayout,
    redirect: '/home',
    children: getAction
  }, {
    path: '/login',
    component: Login
  }, {
    path: '/system_navigation',
    name: 'SystemNavigation',
    component: () => import('../views/SystemNavigation.vue')
  },
  {
    path: '/big_data',
    name: 'BigData',
    component: () => import('../views/bigData/index.vue')
  },
  {
    path: '/',
    name: 'GuanWang',
    component: () => import('../views/GuanWang.vue')
  }
]

const router = new VueRouter({
  routes
})

// 调用路由实例对象的 beforeEach 方法，挂载路由导航守卫
// to是我们跳转的路径，from是来自的路径，next为放行函数
router.beforeEach((to, from, next) => {
  // 用户访问首页路径
  if (to.path === '/home') {
    // 从 localStorage 中获取到保存的token值
    const token = localStorage.getItem('token')
    // 若 token 存在，则放行
    if (token) return next()
    // 若 token 不存在，则跳转到登录页面
    next('/login')
  } else {
    next()
  }
})

export default router
